package com.xiaojie.summer;

import com.alibaba.fastjson.JSONObject;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;
import com.xiaojie.summer.factory.WebApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Map;

/**
 * @author Nick
 * @date 2021/5/24
 * @description
 */
public class SummerServletContextHandler extends AbstractHandler {


    private WebApplicationContext applicationContext = new WebApplicationContext();

    public SummerServletContextHandler() {

    }

    public SummerServletContextHandler(WebApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Override
    public void handle(String s, Request request, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException {
        httpServletResponse.setHeader("Content-type", "application/json;charset=UTF-8");
        httpServletResponse.setCharacterEncoding("UTF-8");
        // 拿到请求路径
        String thisUrl = s;
        // 根据路径去匹配方法
        Map<String, String> requestPathMap = applicationContext.getRequestMap();
        String handlerMethod = requestPathMap.get(thisUrl);
        if (handlerMethod == null || "".equals(handlerMethod)) {
            // 输出文本信息
            httpServletResponse.getWriter().println(JSONObject.toJSONString("没有找到哦"));
            // 这里要标记为已处理
            request.setHandled(true);
            return;
        }
        String[] nameArray = handlerMethod.split("\\.");
        String beanName = nameArray[0];
        String methodName = nameArray[1];

        // 执行方法,并拿到返回数据
        Object beanObject = applicationContext.getBean(beanName);
        Object resultObject = null;

        // String fromCardNo, String toCardNo, int money
        String fromCardNo = httpServletRequest.getParameter("fromCardNo");
        String toCardNo = httpServletRequest.getParameter("toCardNo");
        String money = httpServletRequest.getParameter("money");
        if (fromCardNo == null || toCardNo == null || money == null) {
            System.out.println("fromCardNo: " + fromCardNo);
            System.out.println("toCardNo: " + toCardNo);
            System.out.println("money: " + money);
            // 输出文本信息
            httpServletResponse.getWriter().println(JSONObject.toJSONString("参数不对"));
            // 这里要标记为已处理
            request.setHandled(true);
            return;
        }

        int moneyInt = Integer.parseInt(money);

        // 这里应该是在初始化的时候,通过Param参数进行创建映射类,然后在这里进行执行
        // 找到对应的方法进行执行,返回数据
        Method[] methods = beanObject.getClass().getMethods();
        if (methods != null) {
            for (Method method : methods) {
                String controllerMethodName = method.getName();
                if (methodName.equals(controllerMethodName)) {
                    try {
                        resultObject = method.invoke(beanObject, fromCardNo, toCardNo, moneyInt);
                        httpServletResponse.getWriter().println(JSONObject.toJSONString(resultObject));
                        request.setHandled(true);
                        return;
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            }

        }
        // 输出文本信息
        httpServletResponse.getWriter().println("系统出错了");
        // 这里要标记为已处理
        request.setHandled(true);
    }
}
